package com.haiqiu.system.controller;

import io.swagger.annotations.ApiParam;
import com.haiqiu.common.controller.BaseController;
import com.haiqiu.common.page.PageRequest;
import com.haiqiu.common.result.Constants;
import com.haiqiu.common.result.ResultData;
import com.haiqiu.system.entity.SysPermission;
import com.haiqiu.system.service.SysPermissionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * @author HaiQiu
 * @date 2021/4/2 23:10
 * @desc
 **/
@Api(tags = "菜单权限管理",description = "权限菜单等方法")
@RestController
@RequestMapping("/auth")
public class PermissionController extends BaseController {


    @Autowired
    private SysPermissionService sysPermissionService;


    @ApiOperation(value = "用户令牌解析获取菜单")
    @RequestMapping(method = RequestMethod.POST,value = "/menu")
    public ResultData menu(){
        return decide(sysPermissionService.menu());
    }


    @ApiOperation(value = "菜单列表集合")
    @RequestMapping(method = RequestMethod.POST,value = "/menu/list")
    public ResultData menuList(@RequestBody(required = false) SysPermission sysPermission){
        return decide(sysPermissionService.menuList(sysPermission));
    }

    @ApiOperation(value = "树形菜单权限列表")
    @RequestMapping(method = RequestMethod.POST,value = "/tree")
    public ResultData tree(){
        return decide(sysPermissionService.tree());
    }


    @ApiOperation(value = "角色ID查询菜单ID集合与菜单下拉树结构")
    @RequestMapping(method = RequestMethod.GET,value = "/roleMenuTreeSelect")
    public ResultData roleMenuTreeSelect(@ApiParam(value = "角色ID") @RequestParam Long roleId){
        return decide(sysPermissionService.roleMenuTreeSelect(roleId));
    }

    @ApiOperation(value = "菜单权限新增")
    @RequestMapping(method = RequestMethod.POST,value = "")
    public ResultData save(@RequestBody SysPermission sysPermission){
        sysPermissionService.save(sysPermission);
        return decide(Constants.SUCCESS_ADD);
    }

    @ApiOperation(value = "菜单权限批量删除")
    @RequestMapping(method = RequestMethod.DELETE,value = "")
    public ResultData delete(@ApiParam("批量ID,数组传body") @RequestBody Set<Long> ids){
        return decide(String.format("成功删除%d条数据", sysPermissionService.delete(ids)));
    }

    @ApiOperation(value = "菜单权限批量激活")
    @RequestMapping(method = RequestMethod.PUT,value = "/active")
    public ResultData active(@ApiParam("批量ID,数组传body") @RequestBody List<SysPermission> sysPermissions){
        return decide(String.format("成功激活%d条数据", sysPermissionService.active(sysPermissions)));
    }

    @ApiOperation(value = "菜单权限修改")
    @RequestMapping(method = RequestMethod.PUT,value = "")
    public ResultData update(@RequestBody SysPermission sysPermission){
        sysPermissionService.update(sysPermission);
        return decide(Constants.SUCCESS_UPDATE);
    }

    @ApiOperation(value = "菜单权限查询单条")
    @RequestMapping(method = RequestMethod.GET,value = "")
    public ResultData get(Long id){
        return decide(sysPermissionService.get(id));
    }

    @ApiOperation(value = "菜单权限分页")
    @RequestMapping(method = RequestMethod.POST,value = "/list")
    public ResultData list(@RequestBody PageRequest<SysPermission> request){
        return decide(sysPermissionService.list(request));
    }

    @ApiOperation(value = "菜单权限树形分页")
    @RequestMapping(method = RequestMethod.POST,value = "/tree/list")
    public ResultData treeList(@RequestBody PageRequest<SysPermission> request){
        return decide(sysPermissionService.treeList(request));
    }

    @ApiOperation(value = "菜单权限查询所有")
    @RequestMapping(method = RequestMethod.POST,value = "/all")
    public ResultData all(){
        return decide(sysPermissionService.all());
    }
}
